import cv2
from Origin_Image_Correction import contour_points, img_crroect, contour_points_origin, contour_points_cornor
from xml_read import xml_read, excel_read
from Get_Cell_processed import get_cell_processed,pt
import os

import numpy as np
# import tensorflow as tf
# from skimage.transform import resize
import cv2
# from tensorflow.python import keras
# from keras.layers import Input, BatchNormalization, Activation, Dense, Dropout
# from unet import get_unet
# from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img, save_img
import matplotlib.pyplot as plt

# os.environ['TF_CPP_MIN_LEVEL'] = '2'



# file_images =             "C:\\Users\\wangyu\\Desktop\\program_donqi\\data\\0001\\0001.jpg"
# file_jiaozheng =          "C:\\Users\\wangyu\\Desktop\\program_donqi\\data\\0001\\0001_jiaozheng.jpg"
# excel_path =              "C:\\Users\\wangyu\\Desktop\\program_donqi\\data\\0001\\0001.xlsx"
# OriginCell_save_path =    "C:\\Users\\wangyu\\Desktop\\program_donqi\\data\\0001\\orgin_cells\\"
file_images =             "C:\\Users\\wangyu\\Desktop\\program_donqi\\data\\" + pt + "\\" +pt +".jpg"
file_jiaozheng =          "C:\\Users\\wangyu\\Desktop\\program_donqi\\data\\" + pt + "\\" +pt +"_jiaozheng.jpg"
excel_path =              "C:\\Users\\wangyu\\Desktop\\program_donqi\\data\\" + pt + "\\" +pt +".xlsx"
OriginCell_save_path =    "C:\\Users\\wangyu\\Desktop\\program_donqi\\data\\" + pt + "\\orgin_cells\\"

# 加载图片
img_origin = cv2.imread(file_images)

# 寻找原图中表格角点
contour_point = contour_points_cornor(img_origin)
# contour_point = contour_points_origin(img_origin)
#print("contour_points:", contour_point)

# 校正图像
img_correct = img_crroect(file_jiaozheng, contour_point, img_origin)

# 寻找校正图像中表格角点
contour_points_jiaozheng = contour_points_cornor(img_correct)
#print("jiaozheng:", contour_points_jiaozheng)

# 读取excel表格，获取单元格坐标列表、表格宽、表格高
All_cell_points, weight_table, height_table = excel_read(excel_path)
#print(All_cell_points)

# print("xml文件读取单元格信息", 0, All_cell_points[0], 1, All_cell_points[1], 2, All_cell_points[2], 3, All_cell_points[3], 4,
#       All_cell_points[4], 5, All_cell_points[5], 6, All_cell_points[6])

# split
# 根据excel表格获得的表格高、宽
table_information = [height_table, weight_table]
# print("表格信息：高 宽", table_information)



# 表格分割
img_origin_cells = get_cell_processed(All_cell_points, OriginCell_save_path, img_correct, table_information)




#TODO  mm2、KV2 Rp0.02 Rm 替换

#TODO  去除框线、噪点，锐化图像，提高DPI




